home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / comm / tcp / rxsocket.lha / rxsocket / Examples / amisearch.rexx < prev    next >
OS/2 REXX Batch file  |  1999-05-29  |  4KB  |  177 lines

  1. /* */
  2.  
  3. signal on break_c
  4. signal on syntax
  5. Pname="AmiSearch/4.5"
  6. PRG="AmiSearch"
  7.  
  8. /* open stderr for clean output */
  9. if ~Open("STDERR","CONSOLE:","W") then STDERR = "STDOUT"
  10.  
  11. onAmirc=left(address(),6)=="AMIRC."
  12. TEMPLATE="KEY,TIMEOUT/N,MAXLEN/K/N,SAY/S,HOST/K,URL/S,QUIET/S,BREAK/K/N"
  13. if onAmirc & arg()==1 & arg(1)=="?" then call err TEMPLATE
  14.  
  15. /* parse arguments */
  16. parm.5.value = "ftp.uni-paderborn.de"
  17. if ~ReadArgs(TEMPLATE) then do
  18.     msg=fault(,PRG)
  19.     parse var msg d": " msg
  20.     call err msg
  21. end
  22.  
  23. if parm.7.flag then do
  24.     PRG="AmsBreak"
  25.     task=GetClip("AmiSearch."parm.7.value)
  26.     if task~="" then call Signal(x2d(task),2**12)
  27.     if task~="" then call info "AmiSearch."parm.7.value "broken."
  28.     else call info "AmiSearch."parm.7.value "not found."
  29.     exit
  30. end
  31. else if ~parm.0.flag then call err DosString(116)
  32.  
  33. if ~IsLibOn("SOCKET") then call err "no TCP/IP stack running."
  34.  
  35. do id=0 while GetClip("AmiSearch."id)~="";end;
  36. call SetClip("AmiSearch."id,pragma("ID"))
  37.  
  38. if parm.2.flag then max=parm.2.value
  39. else max=80
  40.  
  41. msg="-----"PRG"."id": '"parm.0.value"' -----"
  42. msgl=length(msg)
  43. call info msg,1
  44.  
  45. /* lookup host */
  46. if ~parm.6.flag then call info "resolving host..."
  47. host="ftp.uni-paderborn.de"
  48. addr=resolve(host)
  49. if addr==-1 then call err "can't lookup host <"host">."
  50.  
  51. /* create a socket */
  52. sock = socket("INET","STREAM")
  53. if sock<0 then call err "can't create my socket."
  54.  
  55. /* remote */
  56. sin.addrFamily = "INET"
  57. sin.addrPort   = 80
  58. sin.addrAddr   = addr
  59.  
  60. /* timeout? */
  61. if parm.1.flag then do
  62.     if parm.1.value<=0 then call err "timeout must be greater than 0."
  63.     tim=CreateTimer()
  64.     ts=TimerSignal(tim)
  65.     call SetSocketSignals(or(ts,2**12))
  66.     call StartTimer(tim,parm.1.value)
  67. end
  68.  
  69. /* connect */
  70. if ~parm.6.flag then call info "connecting..."
  71. if connect(sock,"SIN")<0 then call err "can't connect."
  72.  
  73. /* request to send*/
  74. eol="D0A"x
  75. if ~parm.6.flag then call info "sending request..."
  76. req= "GET /aminetbin/find?"parm.0.value "HTTP/1.0"eol,
  77. "User-Agent:" Pname "(AmigaOS ARexx rxsocket.library)"eol||eol
  78. if send(sock,req)~=length(req) then call err "send error."
  79.  
  80. /* request result */
  81. if ~parm.6.flag then call info "receiving result..."
  82. len=recvline(sock,"BUF")
  83. if len<0 then do
  84.     call err "recv error."
  85.     exit
  86. end
  87. if left(buf,5)~="HTTP/" then do
  88.     call err "bad answer from server."
  89.     exit
  90. end
  91. parse var buf "HTTP/"ver code rest"D0A"x
  92. if code~=200 then do
  93.     call err "server returns an error:"rest "("code")."
  94.     exit
  95. end
  96.  
  97. /* skip head */
  98. go=1
  99. do while go
  100.     len=RecvLine(sock,"BUF")
  101.     if len<0 then call err "recv error."
  102.     parse var buf b": "rest"D0A"x
  103.     go=(buf~=eol)
  104. end
  105.  
  106. /* wait for results */
  107. if ~parm.6.flag then call info copies("-",msgl),1
  108. go=1
  109. do while go
  110.     len=RecvLine(sock,"BUF",256)
  111.     if len<0 then call err "recv error."
  112.     go=(pos("files matching",buf)=0)
  113. end
  114.  
  115. res=word(buf,1)
  116. if res=0 then call err "no match found."
  117. else call info res "file(s) found",1
  118.  
  119. call info left("Name",30) left("Size",5) left("Age",3) "Comment",1
  120. go=1
  121. do forever
  122.     len=recvline(sock,"BUF",1024)
  123.  
  124.     if len<0 then call err "recv error."
  125.     if len=0 then leave
  126.  
  127.     if pos("<a href",buf)~=1 then
  128.         if pos("<hr><a href",buf)~=1 then iterate
  129.  
  130.     parse var buf "a href="u">"file"</a>" path size week "<a href="dummy">" comment "<"
  131.     parse var comment comment "A"x
  132.     if parm.5.flag then do
  133.         if parm.4.flag then u=addpart(parm.4.value,addpart(path,file))
  134.     end
  135.     else u = left(AddPart(path,file),30)
  136.     msg=u left(space(size),5) left(space(week),3) comment
  137.     call info msg,1
  138. end
  139. call info copies("-",msgl),1
  140. call delClip
  141. exit
  142.  
  143. info:
  144. parse arg msg,s
  145.     if onAmirc then
  146.         if s=1 & parm.3.flag then "SAY" msg
  147.         else "ECHO P=" || "1B"x || "b«"PRG"» C=6" msg
  148.     else say PRG":" msg
  149.     return
  150.  
  151. /* print error to stderr */
  152. err:
  153. parse arg msg
  154.     call delClip
  155.     if IsLibOn("SOCKET") then if errno()==4 then msg="timeout"
  156.     if onAmirc then "ECHO P=" || "1B"x || "b«"PRG"» C=6" msg
  157.     else call Writeln(STDERR,PRG":" msg)
  158.     exit
  159.  
  160. /* controll user break */
  161. break_c:
  162.     set.errno=0
  163.     call SetSocketBase("SET")
  164.     call err "user break."
  165.  
  166. syntax:
  167.     msg="Error" ErrorText(rc) "in line" sigl
  168.     if onAmirc then "ECHO P=" || "1B"x || "b«"PRG"» C=6" msg
  169.     else call Writeln(STDERR,PRG":" msg)
  170.     exit
  171.  
  172. delClip:
  173.     call SetClip("AmiSearch."id)
  174.     return
  175.  
  176. /*$VER: AmiSearch.rexx 4.5 (15.5.99)*/
  177.